package com.vision.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vision.mapper.UserFollowMapper;
import com.vision.pojo.vo.PageVO;
import com.vision.pojo.vo.UserFollowVO;
import com.vision.service.UserFollowService;
import com.vision.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserFollowServiceImpl implements UserFollowService {
    @Autowired
    private UserFollowMapper userFollowMapper;

    /**
     * 获取粉丝列表
     * @return 粉丝列表
     */
    @Override
    public PageVO<UserFollowVO> getFansForPage(Integer pageNum, Integer pageSize, Integer userId) {
        // 1. 创建 pageVO
        PageVO<UserFollowVO> pageVO = new PageVO<>();

        // 2. 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        // 3. 查询粉丝列表
        List<UserFollowVO> userFollowVOList = userFollowMapper.selectFansForPage(userId);
        Page<UserFollowVO> page = (Page<UserFollowVO>) userFollowVOList;
        if (userFollowVOList != null) {
            pageVO.setTotal(page.getTotal());
            pageVO.setItems(page.getResult());
        }

        // 5. 返回 pageVO
        return pageVO;
    }

    /**
     * 获取关注列表
     * @return 关注列表
     */
    @Override
    public PageVO<UserFollowVO> getSubscriptionForPage(Integer pageNum, Integer pageSize, Integer userId) {
        // 1. 创建 pageVO
        PageVO<UserFollowVO> pageVO = new PageVO<>();
        // 2. 开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        // 3. 查询关注列表
        List<UserFollowVO> userFollowVOList = userFollowMapper.selectSubscriptionForPage(userId);
        // 4. 向下转型
        Page<UserFollowVO> page = (Page<UserFollowVO>) userFollowVOList;
        // 5. 将查询到的数据封装到 pageVO 中
        if (userFollowVOList != null) {
            pageVO.setTotal(page.getTotal());
            pageVO.setItems(page.getResult());
        }

        // 6. 返回 pageVO
        return pageVO;
    }

    /**
     * 根据粉丝 id 删除粉丝
     * @param followerId 粉丝 id
     */
    @Override
    public void deleteFan(Integer followerId) {
        // 1. 获取当前登录用户 id
        Map<String, Integer> claims = ThreadLocalUtil.get();
        Integer userId = claims.get("id");
        // 2. 删除粉丝
        userFollowMapper.deleteFan(userId,followerId);
    }

    /**
     * 关注用户
     * @param targetUserId 用户 id
     */
    @Override
    public void subscribe(Integer targetUserId) {
        // 1. 获取当前登录用户 id
        Map<String, Integer> claims = ThreadLocalUtil.get();
        Integer userId = claims.get("id");
        // 2. 关注用户
        userFollowMapper.subscribe(userId,targetUserId);
    }

    @Override
    public void cancelSubscribe(Integer targetUserId) {
        // 1. 获取当前登录用户 ID
        Map<String, Integer> claims = ThreadLocalUtil.get();
        Integer userId = claims.get("id");
        // 2. 取消关注
        userFollowMapper.cancelSubscribe(userId,targetUserId);
    }
}
